/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.entity;

import towerz.system.xml.XmlNode;

/**
 *
 * @author LegACy
 */
public class Buff {
	public static final int SPEED_MODIFIER	= 0;
	public static final int ARMOR_MODIFIER	= 1;

	public Buff() {
		//Initialize
		m_Name			= "Buff";
		m_Tint			= 0;
		m_Index			= 0;
		m_Timer			= 0;
		m_Duration		= 0;
		m_Damage		= 0;
		m_DamageDelay	= 0;
		m_DamageTimer	= 0;
		m_Active		= true;

		//Initialize modifier
		m_Modifiers							= new float[Buff.ARMOR_MODIFIER + 1];
		m_Modifiers[Buff.SPEED_MODIFIER]	= 1.0f;
		m_Modifiers[Buff.ARMOR_MODIFIER]	= 1.0f;
	}

	public Buff(XmlNode xml) {
		//Default
		this();
        
        //Parse xml
        for(int i=0; i<xml.getChildren().size(); ++i){
            String Tag      = ((XmlNode)xml.getChildren().elementAt(i)).getNodeName();
            String Value    = ((XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(0)).getNodeValue();
            
            if(Tag.equals("name"))			m_Name = Value;
            else if(Tag.equals("tint"))		m_Tint = Integer.parseInt(Value);
			else if(Tag.equals("duration"))	m_Duration = Long.parseLong(Value);
            else if(Tag.equals("modifier")) {
                XmlNode Child = (XmlNode) xml.getChildren().elementAt(i);
                
                for(int j=0; j<Child.getChildren().size(); ++j){
                    String ChildTag      = ((XmlNode)Child.getChildren().elementAt(j)).getNodeName();
                    String ChildValue    = ((XmlNode)((XmlNode)Child.getChildren().elementAt(j)).getChildren().elementAt(0)).getNodeValue();
                    
                    if(ChildTag.equals("speed"))        m_Modifiers[Buff.SPEED_MODIFIER] = Float.parseFloat(ChildValue);
                    else if(ChildTag.equals("armor"))   m_Modifiers[Buff.ARMOR_MODIFIER] = Float.parseFloat(ChildValue);
                }
            } else if(Tag.equals("poison")){
                XmlNode Child = (XmlNode) xml.getChildren().elementAt(i);
                
                for(int j=0; j<Child.getChildren().size(); ++j){
                    String ChildTag      = ((XmlNode)Child.getChildren().elementAt(j)).getNodeName();
                    String ChildValue    = ((XmlNode)((XmlNode)Child.getChildren().elementAt(j)).getChildren().elementAt(0)).getNodeValue();
                    
                    if(ChildTag.equals("delay"))        m_DamageDelay = Long.parseLong(ChildValue);
                    else if(ChildTag.equals("damage"))  m_Damage = Integer.parseInt(ChildValue);
                }
            }
        }
	}

	//Accessors
	public int getTint()		{	return m_Tint;		}
	public int getIndex()		{	return m_Index;		}
	public long getDuration()	{	return m_Duration;		}
	public boolean isActive()	{	return m_Active;	}

	public float getModifier(int index) {
		return m_Modifiers[index];
	}

	public void update(long time, Enemy enemy) {
		//Increase timer
		m_Timer += time;
		if (m_DamageDelay > 0) m_DamageTimer += time;

		//Is timer more than duration
		if (m_Timer >= m_Duration) {
			//Deactivate
			m_Active = false;
		} else {
			//Check damage timer
			if (m_DamageDelay > 0 && m_DamageTimer > m_DamageDelay) {
				//Damage enemy
				enemy.damage(m_Damage, null);

				//Reduce timer
				m_DamageTimer -= m_DamageDelay;
			}
		}
	}

	//Data
	protected String	m_Name;
	protected int		m_Tint;
	protected int		m_Index;
	protected boolean	m_Active;
	protected long		m_Duration;
	protected long		m_Timer;

	//Poison
	protected int		m_Damage;
	protected long		m_DamageDelay;
	protected long		m_DamageTimer;
	protected float[]	m_Modifiers;
}
